package com.ytjj.qmyx.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.ytjj.common.constants.RedisKeyConstant;
import com.ytjj.common.dto.mqdto.PageStatisticsDto;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.data.constants.MongoCollectionNameConstants;
import com.ytjj.qmyx.data.mapper.StatisticsMapper;
import com.ytjj.qmyx.data.model.mongo.PageStatistics;
//import com.ytjj.qmyx.data.service.KafkaSendService;
import com.ytjj.qmyx.data.service.PageStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 *
 *  *
 **/
@Slf4j
@Service
public class PageStatisticsServiceImpl implements PageStatisticsService {

    @Resource
    private StatisticsMapper statisticsMapper;

//    @Autowired
//    private KafkaSendService kafkaSendService;

    /**
     * 页面路径CODE
     */
    private static Map<String, String> pathCodeMap = null;

    @Resource
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisService redisService;

    @Override
    public void pageStatistics(PageStatisticsDto pageStatisticsDto) {
        //当前记录属性
        PageStatistics pageStatistics = new PageStatistics();
        pageStatistics.copyProperty(pageStatisticsDto);
        String pathCode = getPathCode(pageStatistics.getToPage());
        pageStatistics.setPathCode(pathCode);
        if (org.apache.commons.lang.StringUtils.isEmpty(pageStatistics.getSeq_id())) {
            pageStatistics.setSeq_id(UUID.randomUUID().toString().replace("-", ""));
        }
        if (StringUtils.isEmpty(pageStatistics.getCur_date())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            pageStatistics.setCur_date(sdf.format(new Date()));
        }
//        log.info("页面统计消息pageStatistics:{}",JSONObject.toJSON(pageStatistics));

        mongoTemplate.save(pageStatistics);

        mongoTemplate.save(pageStatistics, MongoCollectionNameConstants.PAGE+pageStatistics.getDayTime());

        //获取自增主键
        Long incrementId = redisService.getIncrementId(RedisKeyConstant.PV_KAFKA_OUT_KEY);
        pageStatistics.setIncrementId(incrementId);
        pageStatistics.setCur_timestamp(System.currentTimeMillis());

//        //发送到kafka
//        try{
//            log.debug("pageStatistics={}", JSONObject.toJSONString(pageStatistics));
//            boolean sendStatus=kafkaSendService.sendPvMsg(JSONObject.toJSONString(pageStatistics));
//            log.debug("sendPvStatus={}",true);
//        }catch (Exception e){
//            log.error("发送到kafka错误",e);
//        }
    }

    /**
     * 获取路径CODE
     *
     * @param page
     * @return
     */
    public String getPathCode(String page) {
        if (pathCodeMap == null) {
            pathCodeMap = Maps.newHashMap();
            List<Map> allPageCode = statisticsMapper.findAllPageCode();
            if (allPageCode != null) {
                for (Map map : allPageCode) {
                    pathCodeMap.put(map.get("path").toString(), map.get("code").toString());
                }
            }
        }
        return pathCodeMap.get(page);
    }

    public void fillPathCodeMap() {
        pathCodeMap = null;
    }

    @Override
    public void testMain(PageStatisticsDto pageStatisticsDto) {
//        getToday(null, null);
    }
}
